package org.wiztools.commons.feed;

import javax.servlet.Servlet;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

class JettyServer {

    private static final Logger LOG = Logger.getLogger(JettyServer.class);

    private final Server server = new Server(10101);

    public void start(Servlet servlet) throws Exception{
        try{
            Context root = new Context(server,"/",Context.SESSIONS);
            root.addServlet(new ServletHolder(servlet), "/*");
            server.setStopAtShutdown(true);
            if(!(server.isStarted() || server.isRunning())){
                server.start();
            }
            LOG.info("Server started");
        }
        catch(Exception ex){
            LOG.fatal(ex);
            throw ex;
        }
    }

    public void stop() throws Exception{
        try{
            server.stop();
            LOG.info("Server stopped");
        }
        catch(Exception ex){
            LOG.fatal(ex);
            throw ex;
        }
    }

}
